﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 栋舍服务接口
    /// </summary>
    public class BuildingManagementService : IBuildingManagementService
    {

        private readonly IBaseRepository<BuildingManagement> repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public BuildingManagementService(IBaseRepository<BuildingManagement> repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="buildingManagement"></param>
        /// <returns></returns>
        public async Task<int> AddBuildingManagement(BuildingManagement buildingManagement)
        {
            return repository.Add(buildingManagement);
        }

        /// <summary>
        /// 单删
        /// </summary>
        /// <param name="buildingManagementId"></param>
        /// <returns></returns>
        public int DelBuildingManagement(int buildingManagementId)
        {
            return repository.Del(buildingManagementId);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="buildingManagementIds"></param>
        /// <returns></returns>
        public int DelRangeBuildingManagement(List<int> buildingManagementIds)
        {
            foreach (var item in buildingManagementIds)
            {
                var list = repository.GetInfoBy(item);
                list.IsDel = 1;
                repository.Update(list);    
            }
            return 1;
        }

        
        /// <summary>
        /// 获取栋舍信息
        /// </summary>
        /// <param name="BuildingName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageOutput<BuildingManagement>> GetBuildingManagement(string? BuildingName, int pageIndex, int pageSize)
        {
            var list = repository.GetAll().Where(c=>c.IsDel==0).ToList();
            if (!string.IsNullOrEmpty(BuildingName))
            {
                list = list.Where(c => c.BuildingName.Contains(BuildingName)).ToList();
            }
            var result = new PageOutput<BuildingManagement>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            var entity = list.OrderByDescending(c => c.BuildingManagementId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = entity;
            return result;
        }
        /// <summary>
        /// 获取单个栋舍信息
        /// </summary>
        /// <param name="buildingManagementId"></param>
        /// <returns></returns>
        public BuildingManagement GetFtBuildingManagement(int buildingManagementId)
        {
            return repository.GetValue(buildingManagementId);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="buildingManagement"></param>
        /// <returns></returns>
        public async Task<int> UpdaBuildingManagement(BuildingManagement buildingManagement)
        {
            return repository.Update(buildingManagement);
        }
    }
}
